package cn.zhaoguang.usedcar.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zhaoguang.usedcar.common.util.PasswordUtil;
import cn.zhaoguang.usedcar.common.util.RsaEncryptUtil;
import cn.zhaoguang.usedcar.main.entity.Manager;
import cn.zhaoguang.usedcar.main.entity.vo.ManagerAddVO;
import cn.zhaoguang.usedcar.main.entity.vo.ManagerUpdateInfoVO;
import cn.zhaoguang.usedcar.main.entity.vo.ManagerUpdatePasswordVO;
import cn.zhaoguang.usedcar.main.mapper.ManagerMapper;
import cn.zhaoguang.usedcar.main.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/7/6 17:52
 */
@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public String add(ManagerAddVO managerAddVO) {
        Manager manager = managerMapper.findManagerByUsername(managerAddVO.getUsername());
        if (manager == null) {
            //获取RSA加密的密码并解密
            String decodePassword = RsaEncryptUtil.privateKeyDecrypt(managerAddVO.getPassword());

            //获取加密盐
            String salt = PasswordUtil.getSalt();
            //使用MD5加密密码用于存入数据库
            String password = PasswordUtil.encode(salt, decodePassword);

            Manager m = new Manager();
            m.setUsername(managerAddVO.getUsername());
            m.setPassword(password);
            m.setEncryptSalt(salt);
            m.setCreateTime(DateUtil.date());
            int result = managerMapper.insertSelective(m);
            if (result > 0) {
                return "添加管理员成功";
            }
            return "添加管理员失败";
        }
        return "添加失败! 管理员已存在";
    }

    @Override
    public int delete(Integer id) {
        return managerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public String updateInfo(ManagerUpdateInfoVO manager) {
        Manager m = new Manager();
        BeanUtil.copyProperties(manager, m);

        int result = managerMapper.updateByPrimaryKeySelective(m);
        if (result > 0) {
            return "管理员信息修改成功";
        }
        return "管理员信息修改失败";
    }

    @Override
    public String updatePassword(ManagerUpdatePasswordVO manager) {
        //获取该管理员id
        Integer id = manager.getId();
        Manager m = managerMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNotEmpty(m)) {
            //旧密码
            String oldPassword = manager.getOldPassword();
            //对比密码
            boolean result = PasswordUtil.matches(m.getEncryptSalt(), oldPassword, m.getPassword());
            if (result) {
                //获取新密码
                String NewPassword = manager.getNewPassword();
                //RSA解密
                String decodePassword = RsaEncryptUtil.privateKeyDecrypt(NewPassword);
                //获取加密盐
                String salt = PasswordUtil.getSalt();
                //使用MD5加密密码后用于存入数据库
                String password = PasswordUtil.encode(salt, decodePassword);

                int res = managerMapper.updatePassword(id, salt, password);
                if (res > 0) {
                    return "密码修改成功";
                }
                return "密码修改失败";
            }
            return "旧密码不正确";
        }
        return "管理员不存在";
    }

    @Override
    public Manager findManagerById(Integer id) {
        return managerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Manager findManagerByUsername(String username) {
        return managerMapper.findManagerByUsername(username);
    }
}
